import { reactive,ref, inject } from "vue";
import { useFlowStore } from '../../store/useFlowStore'

interface Tree {
    id: string;
    label: string;
    value: string;
    children?: Tree[];
}

export const useFlowTypeTable = (currentPage: any, pageSize: any) => {
    //获取API配置
    const API = inject("API") as any;
    const ApiConfig = inject("ApiConfig") as any;
    const flowTypeTable = reactive(
        {
            total: 0,
            tableData: [],
            queryCondition: {
                typeName: '',
            },
            projectFlowTypes:[]
        }
    );
    const flowTypeTree = ref<Tree[]>([]);
    const flowStore = useFlowStore();

    //查询
    const queryData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Flow.GetAllFlowType,
                {
                    pageIndex: currentPage.value,
                    pageSize: pageSize.value,
                    condition: { typeName: flowTypeTable.queryCondition.typeName },
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                flowTypeTable.tableData = response.data.data;
                flowTypeTable.total = response.data.count;
            }
        } catch (error) {
            console.error(error);
        }
    };

    //获取流程类型树
    const getFlowTypeTree = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Flow.GeFlowTypeTree
            );
            
            if (response.status == "200" && response.data.code == "200") {
                const transformMenuData: any = (FlowTypeList: any[]) => {
                    return FlowTypeList.map((t) => {
                        return {
                            id: t.id,
                            label: t.typeName,
                            value: t.typeName,
                            children: t.childFlowTypeList
                                ? transformMenuData(t.childFlowTypeList)
                                : [],
                        };
                    });
                };
                flowTypeTree.value = transformMenuData(response.data.data);
            }
        } catch (error) {
            console.error(error);
        }
    };

    //新增
    const addData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Flow.CreateFlowType,
                flowStore.flowTypeModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //更新
    const updateData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Flow.UpdateFlowType,
                flowStore.flowTypeModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //删除
    const deleteData = async (flowTypeId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Flow.DeleteByFlowTypeId,
                { 'flowTypeId': flowTypeId }
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }

    //获取所有项目流程类型
    const getProjectFlowType = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Flow.GetProjectFlowType
            );
          
            if (response.status == "200" && response.data.code == "200") {                
                flowTypeTable.projectFlowTypes=response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    };


    return { flowTypeTable,flowTypeTree, flowStore, queryData, getFlowTypeTree, addData, updateData, deleteData,getProjectFlowType };
};